package com.yinhe.music.yhmusic.songmenu.presenter;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.songmenu.SongMenuListActivity;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuContract;

import io.reactivex.disposables.Disposable;

/**
 * {@link SongMenuListActivity} P 层
 *
 * @author 何晓华
 */
public class SongMenuPresenter extends BasePresenter<IBaseModel, ISongMenuContract.ISongMenuView>implements ISongMenuContract.ISongMenuPresenter {
    @Override
    public void getSongMenuInfo(int id) {
        ISongMenuContract.ISongMenuView mView = getView();

        if (mView == null) {
            return;
        }


        Disposable disposable = mModel.getSongMenuInfo(id)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(menuList -> mView.setSongMenuUI(menuList), throwable -> mView.showMessage(throwable));

        mDisposable.add(disposable);

    }

    @Override
    public void batchCollection(int menuId, int collectStatus) {
        ISongMenuContract.ISongMenuView mView = getView();

        if (mView == null) {
            return;
        }

        mView.showLoading();

        int[] idList = {menuId};
        String idListString = JsonUtil.getJsonArray(idList);

        if (collectStatus == 0) {
            Disposable collectDisposable = mModel.batchCollection(idListString, null, IConstants.SONGMENU)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                            mView.updateCollectUI(1);
                            mView.showToast(baseInfoResponse.getMsg());
                            RxBus.get().post(RxBusEventType.SongMenu.COLLECT_MENU, RxbusNullObject.INSTANCE);
                        } else {
                            mView.showToast(baseInfoResponse.getMsg());
                        }
                        mView.hideLoading();
                    }, throwable -> {
                        mView.hideLoading();
                        mView.showMessage(throwable);
                    });

            mDisposable.add(collectDisposable);
        } else {
            Disposable unCollectDisposable = mModel.batchUnCollection(idListString,null , IConstants.SONGMENU )
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                            mView.updateCollectUI(0);
                            mView.showToast(baseInfoResponse.getMsg());
                            DataBaseAccessor.getInstance().deleteMenuByIdSyn(menuId);
                            RxBus.get().post(RxBusEventType.SongMenu.DELETE_MENU, RxbusNullObject.INSTANCE);
                        } else {
                            mView.showToast(baseInfoResponse.getMsg());
                        }
                        mView.hideLoading();
                    }, throwable -> {
                        mView.hideLoading();
                        mView.showMessage(throwable);
                    });

            mDisposable.add(unCollectDisposable);
        }
    }
}
